home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Snippets / Equation Evaluator / EE ƒ / EE.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-23  |  26.8 KB  |  809 lines  |  [TEXT/MPCC]

  1. /*************************************************************************
  2. **                                                                       **
  3. ** EE.C         Expression Evaluator                                     **
  4. **                                                                       **
  5. ** AUTHOR:      Mark Morley                                              **
  6. ** COPYRIGHT:   (c) 1992 by Mark Morley                                  **
  7. ** DATE:        December 1991                                            **
  8. ** HISTORY:     Jan 1992 - Made it squash all command line arguments     **
  9. **                         into one big long string.                     **
  10. **                       - It now can set/get VMS symbols as if they     **
  11. **                         were variables.                               **
  12. **                       - Changed max variable name length from 5 to 15 **
  13. **              Jun 1992 - Updated comments and docs                     **
  14. **                                                                          **
  15. **  RMD:        Apr 1995 - Ported to Macintosh for CodeWarrior           **
  16. **                         - Semicolons may now terminate expressions.     **
  17. **                           This now allows several expressions to be     **
  18. **                           given at once.                                 **
  19. **                         - Prototypes                                     **
  20. **                         - Changed function pointer definition for C++     **
  21. **                         - Change int to short                             **
  22. **                         - Fixed crash with undefined functions          **
  23. **                         - SIOUX Console                                  **
  24. **                         - Use PowerPlant exceptions                      **
  25. **                                                                       **
  26. ** You are free to incorporate this code into your own works, even if it **
  27. ** is a commercial application.  However, you may not charge anyone else **
  28. ** for the use of this code!  If you intend to distribute your code,     **
  29. ** I'd appreciate it if you left this message intact.  I'd like to       **
  30. ** receive credit wherever it is appropriate.  Thanks!                   **
  31. **                                                                       **
  32. ** I don't promise that this code does what you think it does...         **
  33. **                                                                       **
  34. ** Please mail any bug reports/fixes/enhancments to me at:               **
  35. **      morley@camosun.bc.ca                                             **
  36. ** or                                                                    **
  37. **      Mark Morley                                                      **
  38. **      3889 Mildred Street                                              **
  39. **      Victoria, BC  Canada                                             **
  40. **      V8Z 7G1                                                          **
  41. **      (604) 479-7861                                                   **
  42. **                                                                       **
  43.  *************************************************************************/
  44.  
  45. /* #define VAX */             /* Uncomment this line if you're using VMS */
  46.  
  47. #include <stdlib.h>
  48. #include <unix.h>
  49. #include <string.h>
  50. #include <math.h>
  51. //#include <setjmp.h>
  52. #include <UException.h>        // from PowerPlant
  53.  
  54. #ifdef VAX
  55. #include <ssdef.h>
  56. #include <descrip.h>
  57. #endif
  58.  
  59. #include "ee.h"
  60.  
  61. #define ERR(n) {ERROR=n; ERPOS=expression-ERANC-1; strcpy(ERTOK,token); Throw(n);}
  62.  
  63. /* These defines only happen if the values are not already defined!  You may
  64.    want to add more precision here, if your machine supports it.
  65. */
  66. #ifndef M_PI
  67. #define M_PI    3.14159265358979323846
  68. #endif
  69. #ifndef M_E
  70. #define M_E     2.71828182845904523536
  71. #endif
  72.  
  73.  
  74. /*************************************************************************
  75. **                                                                       **
  76. ** PROTOTYPES FOR CUSTOM MATH FUNCTIONS                                  **
  77. **                                                                       **
  78.  *************************************************************************/
  79.  
  80. double deg( double x );
  81. double rad( double x );
  82.  
  83.  
  84. /*************************************************************************
  85. **                                                                       **
  86. ** VARIABLE DECLARATIONS                                                 **
  87. **                                                                       **
  88.  *************************************************************************/
  89.  
  90. short   ERROR;                 /* The error number */
  91. char    ERTOK[TOKLEN + 1];     /* The token that generated the error */
  92. short   ERPOS;                 /* The offset from the start of the expression */
  93. char*    ERANC;                 /* Used to calculate ERPOS */
  94.  
  95. /*
  96.    Add any "constants" here...  These are "read-only" values that are
  97.    provided as a convienence to the user.  Their values can not be
  98.    permanently changed.  The first field is the variable name, the second
  99.    is its value.
  100. */
  101. VARIABLE Consts[] =
  102. {
  103.    /* name, value */
  104.    { "pi",      M_PI },
  105.    { "e",       M_E },
  106.  
  107.    { 0 }
  108. };
  109.  
  110. /*
  111.    Add any math functions that you wish to recognise here...  The first
  112.    field is the name of the function as it would appear in an expression.
  113.    The second field tells how many arguments to expect.  The third is
  114.    a pointer to the actual function to use.
  115.    
  116.    FunctionPtr definition added by RMD
  117. */
  118. FUNCTION Funcs[] =
  119. {
  120.    /* name, funtion to call */
  121.    { "sin",     1,    (FunctionPtr)sin },
  122.    { "cos",     1,    (FunctionPtr)cos },
  123.    { "tan",     1,    (FunctionPtr)tan },
  124.    { "asin",    1,    (FunctionPtr)asin },
  125.    { "acos",    1,    (FunctionPtr)acos },
  126.    { "atan",    1,    (FunctionPtr)atan },
  127.    { "sinh",    1,    (FunctionPtr)sinh },
  128.    { "cosh",    1,    (FunctionPtr)cosh },
  129.    { "tanh",    1,    (FunctionPtr)tanh },
  130.    { "exp",     1,    (FunctionPtr)exp },
  131.    { "log",     1,    (FunctionPtr)log },
  132.    { "log10",   1,    (FunctionPtr)log10 },
  133.    { "sqrt",    1,    (FunctionPtr)sqrt },
  134.    { "floor",   1,    (FunctionPtr)floor },
  135.    { "ceil",    1,    (FunctionPtr)ceil },
  136.    { "abs",     1,    (FunctionPtr)fabs },
  137.    { "hypot",   2,    (FunctionPtr)hypot },
  138.  
  139.    { "deg",     1,    (FunctionPtr)deg },
  140.    { "rad",     1,    (FunctionPtr) rad },
  141.  
  142.    { "",        0,        nil }
  143. };
  144.  
  145. VARIABLE        Vars[MAXVARS];       /* Array for user-defined variables */
  146. char*              expression;          /* Pointer to the user's expression */
  147. char               token[TOKLEN + 1];   /* Holds the current token */
  148. short           type;                /* Type of the current token */
  149. jmp_buf         jb;                  /* jmp_buf for errors */
  150.  
  151.  
  152. /*************************************************************************
  153. **                                                                       **
  154. ** Some custom math functions...   Note that they must be prototyped     **
  155. ** above (if your compiler requires it)                                  **
  156. **                                                                       **
  157. ** deg( x )             Converts x radians to degrees.                   **
  158. ** rad( x )             Converts x degrees to radians.                   **
  159. **                                                                       **
  160.  *************************************************************************/
  161.  
  162. double
  163. deg( double x )
  164. {
  165.    return( x * 180.0 / M_PI );
  166. }
  167.  
  168. double
  169. rad( double x )
  170. {
  171.    return( x * M_PI / 180.0 );
  172. }
  173.  
  174.  
  175. /*************************************************************************
  176. **                                                                       **
  177. ** GetSymbol( char* s )                                                  **
  178. **                                                                       **
  179. ** This routine obtains a value from the program's environment.          **
  180. ** This works for DOS and VMS (and other OS's???)
  181. **                                                                       **
  182.  ************************************************************************/
  183.  
  184. short GetSymbol( char* s, TYPE* v )
  185. {
  186.    char* e;
  187.  
  188.    e = getenv( s );
  189.    if( !e )
  190.       return( 0 );
  191.    *v = atof( e );
  192.    return( 1 );
  193. }
  194.  
  195.  
  196. /*************************************************************************
  197. **                                                                       **
  198. ** SetSymbol( char* s, char* v )                                         **
  199. **                                                                       **
  200. ** This VMS specific routine sets (or updates) a VMS symbol to a given   **
  201. ** value                                                                 **
  202. **                                                                       **
  203.  *************************************************************************/
  204.  
  205. #ifdef VAX
  206. SetSymbol( char* s, char* v )
  207. {
  208.    struct dsc$descriptor_s sym;
  209.    struct dsc$descriptor_s val;
  210.    long                    typ = 1;
  211.  
  212.    sym.dsc$w_length = strlen( s );
  213.    sym.dsc$a_pointer = s;
  214.    sym.dsc$b_class = DSC$K_CLASS_S;
  215.    sym.dsc$b_dtype = DSC$K_DTYPE_T;
  216.    val.dsc$w_length = strlen( v );
  217.    val.dsc$a_pointer = v;
  218.    val.dsc$b_class = DSC$K_CLASS_S;
  219.    val.dsc$b_dtype = DSC$K_DTYPE_T;
  220.    return( LIB$SET_SYMBOL( &sym, &val, &typ ) );
  221. }
  222. #endif
  223.  
  224.  
  225. /*************************************************************************
  226. **                                                                       **
  227. ** strlwr( char* s )   Internal use only                                 **
  228. **                                                                       **
  229. ** This routine converts a string to lowercase.  I know many compilers   **
  230. ** offer their own routine, but my VMS compiler didn't so...             **
  231. ** Again, this one is ASCII specific!                                    **
  232. **                                                                       **
  233.  *************************************************************************/
  234.  
  235. static void
  236. strlwr( char* s )
  237. {
  238.    while( *s )
  239.    {
  240.       if( *s >= 'A' && *s <= 'Z' )
  241.          *s += 32;
  242.       s++;
  243.    }
  244. }
  245.  
  246.  
  247. /*************************************************************************
  248. **                                                                       **
  249. ** ClearAllVars()                                                        **
  250. **                                                                       **
  251. ** Erases all user-defined variables from memory. Note that constants    **
  252. ** can not be erased or modified in any way by the user.                 **
  253. **                                                                       **
  254. ** Returns nothing.                                                      **
  255. **                                                                       **
  256.  *************************************************************************/
  257.  
  258. void ClearAllVars(void)
  259. {
  260.    short i;
  261.  
  262.    for( i = 0; i < MAXVARS; i++ )
  263.    {
  264.       *Vars[i].name = 0;
  265.       Vars[i].value = 0;
  266.    }
  267. }
  268.  
  269.  
  270. /*************************************************************************
  271. **                                                                       **
  272. ** ClearVar( char* name )                                                **
  273. **                                                                       **
  274. ** Erases the user-defined variable that is called NAME from memory.     **
  275. ** Note that constants are not affected.                                 **
  276. **                                                                       **
  277. ** Returns 1 if the variable was found and erased, or 0 if it didn't     **
  278. ** exist.                                                                **
  279. **                                                                       **
  280.  *************************************************************************/
  281.  
  282. Boolean ClearVar( char* name )
  283. {
  284.    short i;
  285.  
  286.    for( i = 0; i < MAXVARS; i++ )
  287.       if( *Vars[i].name && ! strcmp( name, Vars[i].name ) )
  288.       {
  289.          *Vars[i].name = 0;
  290.          Vars[i].value = 0;
  291.          return true;
  292.       }
  293.    return false;
  294. }
  295.  
  296.  
  297. /*************************************************************************
  298. **                                                                       **
  299. ** GetValue( char* name, TYPE* value )                                   **
  300. **                                                                       **
  301. ** Looks up the specified variable (or constant) known as NAME and       **
  302. ** returns its contents in VALUE.                                        **
  303. **                                                                       **
  304. ** First the user-defined variables are searched, then the constants are **
  305. ** searched.                                                             **
  306. **                                                                       **
  307. ** Returns 1 if the value was found, or 0 if it wasn't.                  **
  308. **                                                                       **
  309.  *************************************************************************/
  310.  
  311. Boolean GetValue( char* name, TYPE* value )
  312. {
  313.    short i;
  314.  
  315.    /* First check for an environment variable reference... */
  316.    if( *name == '_' )
  317.       return( GetSymbol( name + 1, value ) );
  318.  
  319.    /* Now check the user-defined variables. */
  320.    for( i = 0; i < MAXVARS; i++ )
  321.       if( *Vars[i].name && ! strcmp( name, Vars[i].name ) )
  322.       {
  323.          *value = Vars[i].value;
  324.          return true;
  325.       }
  326.  
  327.    /* Now check the programmer-defined constants. */
  328.    for( i = 0; *Consts[i].name; i++ )
  329.       if( *Consts[i].name && ! strcmp( name, Consts[i].name ) )
  330.       {
  331.          *value = Consts[i].value;
  332.          return true;
  333.       }
  334.    return false;
  335. }
  336.  
  337.  
  338. /*************************************************************************
  339. **                                                                       **
  340. ** SetValue( char* name, TYPE* value )                                   **
  341. **                                                                       **
  342. ** First, it erases any user-defined variable that is called NAME.  Then **
  343. ** it creates a new variable called NAME and gives it the value VALUE.   **
  344. **                                                                       **
  345. ** Returns 1 if the value was added, or 0 if there was no more room.     **
  346. **                                                                       **
  347.  *************************************************************************/
  348.  
  349. Boolean SetValue( char* name, TYPE* value )
  350. {
  351.    char b[30];
  352.    short  i;
  353.  
  354. #ifdef VAX
  355.    if( *name == '_' )
  356.    {
  357.       sprintf( b, "%g", *value );
  358.       if( SetSymbol( name + 1, b ) != SS$_NORMAL )
  359.          return false;
  360.       return true;
  361.    }
  362. #endif
  363.    ClearVar( name );
  364.    for( i = 0; i < MAXVARS; i++ )
  365.       if( ! *Vars[i].name )
  366.       {
  367.          strcpy( Vars[i].name, name );
  368.          Vars[i].name[VARLEN] = 0;
  369.          Vars[i].value = *value;
  370.          return true;
  371.       }
  372.    return false;
  373. }
  374.  
  375.  
  376. /*************************************************************************
  377. **                                                                       **
  378. ** Parse()   Internal use only                                           **
  379. **                                                                       **
  380. ** This function is used to grab the next token from the expression that **
  381. ** is being evaluated.                                                   **
  382. **                                                                       **
  383.  *************************************************************************/
  384.  
  385. static void
  386. Parse(void)
  387. {
  388.    char* t;
  389.  
  390.    type = 0;
  391.    t = token;
  392.    if (*expression == ';') return;
  393.    while( iswhite( *expression ) )
  394.       expression++;
  395.    if( isdelim( *expression ) )
  396.    {
  397.       type = DEL;
  398.       *t++ = *expression++;
  399.    }
  400.    else if( isnumer( *expression ) )
  401.    {
  402.       type = NUM;
  403.       while( isnumer( *expression ) )
  404.          *t++ = *expression++;
  405.    }
  406.    else if( isalpha( *expression ) )
  407.    {
  408.       type = VAR;
  409.       while( isalpha( *expression ) )
  410.         *t++ = *expression++;
  411.       token[VARLEN] = 0;
  412.    }
  413.    else if( *expression )
  414.    {
  415.       *t++ = *expression++;
  416.       *t = 0;
  417.       ERR( E_SYNTAX );
  418.    }
  419.    *t = 0;
  420.    while( iswhite( *expression ) )
  421.       expression++;
  422. }
  423.  
  424.  
  425. /*************************************************************************
  426. **                                                                       **
  427. ** Level1( TYPE* r )   Internal use only                                 **
  428. **                                                                       **
  429. ** This function handles any variable assignment operations.             **
  430. ** It returns a value of 1 if it is a top-level assignment operation,    **
  431. ** otherwise it returns 0                                                **
  432. **                                                                       **
  433.  *************************************************************************/
  434.  
  435. static Boolean
  436. Level1( TYPE* r )
  437. {
  438.    char t[VARLEN + 1];
  439.  
  440.    if( type == VAR )
  441.       if( *expression == '=' )
  442.       {
  443.          strcpy( t, token );
  444.          Parse();
  445.          Parse();
  446.          if( !*token )
  447.          {
  448.             ClearVar( t );
  449.             return(1);
  450.          }
  451.          Level2( r );
  452.          if( ! SetValue( t, r ) )
  453.             ERR( E_MAXVARS );
  454.          return( 1 );
  455.       }
  456.    Level2( r );
  457.    return( 0 );
  458. }
  459.  
  460.  
  461. /*************************************************************************
  462. **                                                                       **
  463. ** Level2( TYPE* r )   Internal use only                                 **
  464. **                                                                       **
  465. ** This function handles any addition and subtraction operations.        **
  466. **                                                                       **
  467.  *************************************************************************/
  468.  
  469. static void
  470. Level2( TYPE* r )
  471. {
  472.    TYPE t = 0;
  473.    char o;
  474.  
  475.    Level3( r );
  476.    while( (o = *token) == '+' || o == '-' )
  477.    {
  478.       Parse();
  479.       Level3( &t );
  480.       if( o == '+' )
  481.          *r = *r + t;
  482.       else if( o == '-' )
  483.          *r = *r - t;
  484.    }
  485. }
  486.  
  487.  
  488. /*************************************************************************
  489. **                                                                       **
  490. ** Level3( TYPE* r )   Internal use only                                 **
  491. **                                                                       **
  492. ** This function handles any multiplication, division, or modulo.        **
  493. **                                                                       **
  494.  *************************************************************************/
  495.  
  496. static void
  497. Level3( TYPE* r )
  498. {
  499.    TYPE t;
  500.    char o;
  501.  
  502.    Level4( r );
  503.    while( (o = *token) == '*' || o == '/' || o == '%' )
  504.    {
  505.       Parse();
  506.       Level4( &t );
  507.       if( o == '*' )
  508.          *r = *r * t;
  509.       else if( o == '/' )
  510.       {
  511.          if( t == 0 )
  512.             ERR( E_DIVZERO );
  513.          *r = *r / t;
  514.       }
  515.       else if( o == '%' )
  516.       {
  517.          if( t == 0 )
  518.             ERR( E_DIVZERO );
  519.          *r = fmod( *r, t );
  520.       }
  521.    }
  522. }
  523.  
  524.  
  525. /*************************************************************************
  526. **                                                                       **
  527. ** Level4( TYPE* r )   Internal use only                                 **
  528. **                                                                       **
  529. ** This function handles any "to the power of" operations.               **
  530. **                                                                       **
  531.  *************************************************************************/
  532.  
  533. static void
  534. Level4( TYPE* r )
  535. {
  536.    TYPE t;
  537.  
  538.    Level5( r );
  539.    if( *token == '^' )
  540.    {
  541.       Parse();
  542.       Level5( &t );
  543.       *r = pow( *r, t );
  544.    }
  545. }
  546.  
  547.  
  548. /*************************************************************************
  549. **                                                                       **
  550. ** Level5( TYPE* r )   Internal use only                                 **
  551. **                                                                       **
  552. ** This function handles any unary + or - signs.                         **
  553. **                                                                       **
  554.  *************************************************************************/
  555.  
  556. static void
  557. Level5( TYPE* r )
  558. {
  559.    char o = 0;
  560.  
  561.    if( *token == '+' || *token == '-' )
  562.    {
  563.       o = *token;
  564.       Parse();
  565.    }
  566.    Level6( r );
  567.    if( o == '-' )
  568.       *r = -*r;
  569. }
  570.  
  571.  
  572. /*************************************************************************
  573. **                                                                       **
  574. ** Level6( TYPE* r )   Internal use only                                 **
  575. **                                                                       **
  576. ** This function handles any literal numbers, variables, or functions.   **
  577. **                                                                       **
  578.  *************************************************************************/
  579.  
  580. static void
  581. Level6( TYPE* r )
  582. {
  583.    short  i;
  584.    short  n;
  585.    TYPE a[3];
  586.  
  587.    if( *token == '(' )
  588.    {
  589.       Parse();
  590.       if( *token == ')' )
  591.          ERR( E_NOARG );
  592.       Level1( r );
  593.       if( *token != ')' )
  594.          ERR( E_UNBALAN );
  595.       Parse();
  596.    }
  597.    else
  598.    {
  599.       if( type == NUM )
  600.       {
  601.          *r = (TYPE) atof( token );
  602.          Parse();
  603.       }
  604.       else if( type == VAR )
  605.       {
  606.          if( *expression == '(' )
  607.          {
  608.             for( i = 0; Funcs[i].args; i++ )
  609.                if( ! strcmp( token, Funcs[i].name ) )
  610.                {
  611.                   Parse();
  612.                   n = 0;
  613.                   do
  614.                   {
  615.                      Parse();
  616.                      if( *token == ')' || *token == ',' )
  617.                         ERR( E_NOARG );
  618.                      a[n] = 0;
  619.                      Level1( &a[n] );
  620.                      n++;
  621.                   } while( n < 4 && *token == ',' );
  622.                   Parse();
  623.                   if( n != Funcs[i].args )
  624.                   {
  625.                      strcpy( token, Funcs[i].name );
  626.                      ERR( E_NUMARGS );
  627.                   }
  628.                   *r = Funcs[i].func( a[0], a[1], a[2] );
  629.                   return;
  630.                }
  631.                if( ! *Funcs[i].name )
  632.                   ERR( E_BADFUNC );
  633.             }
  634.             else if( ! GetValue( token, r ) )
  635.                ERR( E_UNKNOWN );
  636.          Parse();
  637.       }
  638.       else
  639.          ERR( E_SYNTAX );
  640.    }
  641. }
  642.  
  643.  
  644. /*************************************************************************
  645. **                                                                       **
  646. ** Evaluate( char* e, TYPE* result, short* a )                             **
  647. **                                                                       **
  648. ** This function is called to evaluate the expression E and return the   **
  649. ** answer in RESULT.  If the expression was a top-level assignment, a    **
  650. ** value of 1 will be returned in A, otherwise it will contain 0.        **
  651. **                                                                       **
  652. ** Returns E_OK if the expression is valid, or an error code.            **
  653. **                                                                       **
  654.  *************************************************************************/
  655.  
  656.  
  657.  
  658. Boolean Evaluate( char* e, TYPE* result, short* a )
  659. {
  660. #ifdef __MWERKS__
  661.     Try_ {
  662. #else
  663.    if( setjmp( jb ) )
  664.       return( ERROR );
  665. #endif
  666.    expression = e;
  667.    ERANC = e;
  668.    strlwr( expression );
  669.    *result = 0;
  670.    Parse();
  671.    if( ! *token )
  672.       ERR( E_EMPTY );
  673.    *a = Level1( result );
  674. #ifdef __MWERKS__
  675.     }
  676.     Catch_(err) {
  677.           return( err );
  678.     } EndCatch_
  679. #endif
  680.    return( E_OK );
  681. }
  682.  
  683.  
  684. /*************************************************************************
  685. **                                                                       **
  686. ** What follows is a main() routine that evaluates the command line      **
  687. ** arguments one at a time, and displays the results of each expression. **
  688. ** Without arguments, it becomes an interactive calculator.              **
  689. **                                                                       **
  690.  *************************************************************************/
  691.  
  692. #include <stdio.h>
  693.  
  694. char* ErrMsgs[] =
  695. {
  696.    "Syntax error",
  697.    "Unbalanced parenthesis",
  698.    "Division by zero",
  699.    "Unknown variable",
  700.    "Maximum variables exceeded",
  701.    "Unrecognised funtion",
  702.    "Wrong number of arguments to funtion",
  703.    "Missing an argument",
  704.    "Empty expression"
  705. };
  706.  
  707. main( short argc, char* argv[] )
  708. {
  709.    TYPE  result;
  710.    short   i;
  711.    short   ec;
  712.    short   a;
  713.    char  line[1024];
  714.    char* v;
  715.  
  716.    ClearAllVars();
  717.  
  718. /*
  719.  
  720. RMD    -    not useful on the mac, so out it comes.
  721.  
  722. //  If we have command line arguments then we evaluate them and exit
  723.    if( argc > 1 )
  724.    {
  725.       // Concatenate all arguments into one string
  726.       strcpy( line, argv[1] );
  727.       for( i = 2; i < argc; i++ )
  728.          strcat( line, argv[i] );
  729.  
  730.       // Call the evaluator
  731.       if( (ec = Evaluate( line, &result, &a )) == E_OK )
  732.       {
  733.          // If we didn't assign a variable, then print the result
  734.          if( ! a )
  735.             printf( "%g\n", result );
  736.       }
  737.       else if( ec != E_EMPTY )
  738.       {
  739.          / Display error info.  In this example, an E_EMPTY error is ignored
  740.          printf( "ERROR: %s - %s", ErrMsgs[ERROR - 1], ERTOK );
  741.          printf( "\n%s", ERANC );
  742.          printf( "\n%*s^\n", ERPOS, "" );
  743.       }
  744.       return;
  745.    }
  746. */
  747.    /* There were no command line arguments, so lets go interactive. */
  748.    printf( "\nEE - Equation Evaluator" );
  749.    printf( "\nBy Mark Morley  December 1991" );
  750. //   printf( "\nEnter EXIT to quit.\n" );
  751.    printf( "\nEE> " );
  752.  
  753.    /* Input one line at a time from the user.  Note that it uses stdin, so on
  754.       DOS or UNIX you could pipe a list of expressions into it... */
  755.    for( gets( line ); !feof( stdin ); gets( line ) )
  756.    {
  757.       strlwr( line );
  758.  
  759.  //      /* Did the user ask to exit? */
  760.  //     if( ! strcmp( line, "exit" ) )
  761.  //        return;
  762.  
  763.       /* Did the user ask to see the variables in memory? */
  764.      if( ! strcmp( line, "vars" ) )
  765.       {
  766.          for( i = 0; i < MAXVARS; i++ )
  767.             if( *Vars[i].name )
  768.                printf( "%s = %g\n", Vars[i].name, Vars[i].value );
  769.       }
  770.  
  771.       /* Did the user ask to see the constants in memory? */
  772.       else if( ! strcmp( line, "cons" ) )
  773.       {
  774.          for( i = 0; *Consts[i].name; i++ )
  775.             printf( "%s = %g\n", Consts[i].name, Consts[i].value );
  776.       }
  777.  
  778.       /* Did the user ask to clear all variables? */
  779.       else if( ! strcmp( line, "clr" ) )
  780.          ClearAllVars();
  781.  
  782.       /* If none of the above, then we attempt to evaluate the user's input. */
  783.       else
  784.       {
  785.          char *ee = line;
  786.          while (*ee != 0) {
  787.               /* Call the evaluator. */
  788.              if( (ec = Evaluate( ee, &result, &a )) == E_OK )
  789.              {
  790.                 /* Only display the result if it was not an assignment. */
  791.                 if( ! a )
  792.                    printf( "%g\n", result );
  793.              }
  794.              else if( ec != E_EMPTY )
  795.              {
  796.                 /* Display error information.  E_EMPTY is ignored. */
  797.                 printf( "ERROR: %s - %s", ErrMsgs[ERROR - 1], ERTOK );
  798.                 printf( "\n%s", ERANC );
  799.                 printf( "\n%*s^\n", ERPOS, "" );
  800.              }
  801.              ee = expression;
  802.              while ((*ee == ';') || iswhite( *ee ))
  803.                  ee++;
  804.             }    
  805.           }
  806.       printf( "EE> " );
  807.    }
  808. }
  809.